package cn.biplam.back.operator.systemManage.sysprivi.menu;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.SysManageCode.MenuOptCode;
import cn.biplam.common.struc.SysStruc.MenuStruc;
import cn.biplam.common.struc.SysStruc.UserPriviStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  系统菜单管理数据操作类
 * @author XiaoRuxing Dec 8, 20097:12:04 PM
 * @version 1.0
 */
public class MenuOpt extends DataOperator{

	public MenuOpt() throws ErrorMessage {
		super(MenuStruc.Table_Name);
	}

	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(opId.equals(MenuOptCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(opId.equals(MenuOptCode.Del)){
			this.delete(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(opId.equals(MenuOptCode.Update)){
			this.update(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(opId.equals(MenuOptCode.Query_By_ID)){
			this.queryByID(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(opId.equals(MenuOptCode.Query_UsersMenu)){
			this.queryUsersMenu(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}

	/**
	 *  根据菜单ID查询菜单
	 */
	private void queryByID(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(MenuStruc.MENU_ID);
		sp.append("='");
		sp.append(filter.getValue());
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 *  插入菜单
	 * @throws ErrorMessage 
	 */
	private void insert(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		//菜单ID
		String menuId = filter.getValue(MenuStruc.MENU_ID);
		if(menuId == null){
			menuId = this.generatorRowID();
		}else{
			this.result.setLastRowId(menuId);
		}
		insert.putStringFieldDenyNull(MenuStruc.MENU_ID, menuId);
		insert.putStringFieldIgnoreNull(MenuStruc.MENU_PARENT, filter.getValue(MenuStruc.MENU_PARENT));
		insert.putStringFieldIgnoreNull(MenuStruc.MENU_NAME, filter.getValue(MenuStruc.MENU_NAME));
		insert.putStringFieldIgnoreNull(MenuStruc.MENU_DESC, filter.getValue(MenuStruc.MENU_DESC));
		insert.putFieldIgnoreNull(MenuStruc.MENU_SEQUENCE, filter.getValue(MenuStruc.MENU_SEQUENCE));
		insert.putStringFieldIgnoreNull(MenuStruc.PIC_PATH, filter.getValue(MenuStruc.PIC_PATH));
		insert.putStringFieldIgnoreNull(MenuStruc.ACTION_ID, filter.getValue(MenuStruc.ACTION_ID));
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 *  修改菜单
	 * @throws ErrorMessage 
	 */
	private void update(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(MenuStruc.MENU_PARENT, filter.getValue(MenuStruc.MENU_PARENT));
		update.putStringFieldIgnoreNull(MenuStruc.MENU_NAME, filter.getValue(MenuStruc.MENU_NAME));
		update.putStringFieldIgnoreNull(MenuStruc.MENU_DESC, filter.getValue(MenuStruc.MENU_DESC));
		update.putStringFieldIgnoreNull(MenuStruc.PIC_PATH, filter.getValue(MenuStruc.PIC_PATH));
		update.putStringFieldIgnoreNull(MenuStruc.ACTION_ID, filter.getValue(MenuStruc.ACTION_ID));
		update.putFieldIgnoreNull(MenuStruc.MENU_SEQUENCE, filter.getValue(MenuStruc.MENU_SEQUENCE));
		update.setWhere(MenuStruc.MENU_ID+"='"+filter.getValue(MenuStruc.MENU_ID)+"'");
		sql.add(update.getSimpleSql());
	}
	
	/**
	 *  删除菜单
	 * @throws ErrorMessage 
	 */
	private void delete(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		Delete delete = new Delete(this.tableName);
		delete.setWhere(MenuStruc.MENU_ID+"='"+filter.getValue(MenuStruc.MENU_ID)+"'");
		sql.add(delete.getSimpleSql());
	}
	
	private void queryUsersMenu(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		StringBuilder sb = new StringBuilder();
		sb.append("select a.* from ");
		sb.append(filter.getValue("tableName"));
		sb.append(" a join ");
		sb.append(UserPriviStruc.Table_Name);
		sb.append(" b on a.");
		sb.append(MenuStruc.MENU_ID);
		sb.append("= b.");
		sb.append(UserPriviStruc.PRIVI_ID);

		sb.append(" where ");
		sb.append(UserPriviStruc.USER_ID);
		sb.append("='");
		sb.append(this.userId);
		sb.append("' and ");

		sb.append(MenuStruc.MENU_PARENT);
		sb.append("='");
		sb.append(filter.getValue());
		sb.append("' order by ");
		sb.append(MenuStruc.MENU_SEQUENCE);
		sql.add(sb.toString());
	}
}
